package com.spider.silence.worker;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.spider.silence.downloader.SinaWeiBoDownLoader;
import com.spider.silence.downloader.TencentWeiBoDownloader;
import com.spider.silence.output.FileWriter;
import com.spider.silence.output.FileWriterPool;
import com.spider.silence.output.MongoWriter;
import com.spider.silence.output.MongoWriterPool;
import com.spider.silence.pool.SinaWeiBoDownLoaderPool;
import com.spider.silence.pool.TencentWeiBoDownLoaderPool;
import com.spider.silence.task.SinaWeiBoTask;
import com.spider.silence.task.TencentWeiBoTask;
import com.spider.silence.task.manager.CommonTaskManager;
import com.spider.silence.utils.DateUtils;
import org.apache.log4j.Logger;

import java.io.File;
import java.util.Map;

/**
 * @author liyuanguo
 * @version V1.0
 * @time 2017/3/2 9:51
 * @description
 */
public class SinaWeiBoWorker  implements Runnable{
    private Logger logger = Logger.getLogger(SinaWeiBoWorker.class);
    @Override
    public void run() {
        while(true&&!Thread.interrupted()){
            SinaWeiBoDownLoader downLoader=null;
            SinaWeiBoDownLoaderPool downLoaderPool= SinaWeiBoDownLoaderPool.getInstance();
            FileWriter writer = null;
            FileWriterPool writerPool =FileWriterPool.getInstance();
            try {
                while(true){
                    downLoader=(SinaWeiBoDownLoader)downLoaderPool.fetchDownLoader(1000);
                    if(null!=downLoader){
                        break;
                    }
                }
                SinaWeiBoTask task=(SinaWeiBoTask) CommonTaskManager.takeTask("sinaWeiBo",1000);
                if(null!=downLoader&&null!=task){
                    JSONObject content =downLoader.crawlItem(task.getItemId());
                    String filePrefix="SinaWeiBo";//文件前缀
                    JSONObject data=content.getJSONObject("data");
                    if(null!=data&&!"".equals(data)&&data.size()>0) {
                        System.out.println("shuju："+content);
                        long tsp = DateUtils.dateToTimestamp(DateUtils.parseDateTime(data.getString("publish_time"))).getTime();
                        String user_id = data.getString("publish_user_id");
                        String tid = data.getString("id");
                        writer= writerPool.fetchWriter();
                        String fileName = filePrefix + "_" + tsp + "_" + user_id + "_" + tid;//平台名称（或者英文）_ 言论发布的时间戳 _ 发布用户的ID _ 言论的tid
                        String publishDate = DateUtils.formatDate(DateUtils.parseDate(data.getString("publish_time"))).replace("-", "-");
                        Map<String,File> map=downLoader.getFiles();
                        writer.write("新浪微博_speeches_" + publishDate, fileName, JSON.toJSONString(content, SerializerFeature.BrowserCompatible),map);
                    }
                }
            } catch (Exception e) {
                logger.error(e.getMessage(),e);
                continue;
            }finally {
                if(null!=downLoader){
                    downLoaderPool.releaseDownLoader(downLoader);
                }
                if(null!=writer){
                    writerPool.releaseWriter(writer);
                }
            }
        }

    }
}
